<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\room\Lists;
use app\common\model\room\Users;
use app\common\model\room\Users as UR;
use app\common\model\user\Bindgame;

class Room extends Api
{
    protected $noNeedLogin = ["lists"];
    protected $noNeedRight = "*";

    public function lists()
    {
        $pagesize = $this->request->param("size");
        $game_id = $this->request->param("game_id");
        $modes = $this->request->param("modes_id");
        $status = $this->request->param("status", 0);
        $model = new Lists();
        $model->with(['game', 'levels', 'modes', 'subarea']);
        $model->where("lists.status", "neq", 2);
        $model->where('lists.status', $status);
        if ($game_id) {
            $model->where('lists.game_id', $game_id);
        }
        if ($modes) {
            $model->where("lists.modes_id", $modes);
        }
        $data = $model->where("lists.status", "neq", 3)->order('lists.updatetime DESC')->paginate($pagesize);

        foreach ($data as $row) {
            $row->getRelation("game")->visible(["name", "logo"]);
            $row->getRelation("levels")->visible(["money", "vip_ids"]);
            $row->getRelation("modes")->visible(["name"]);
            $row->getRelation("subarea")->visible(["name"]);
        }
        $this->success("", $data);
    }

    //房间信息
    public function info()
    {
        $id = $this->request->param("id");
        if (!$id) {
            $this->error("当前房间号有误");
        }
        $room = Lists::with(['game', 'levels', 'modes', 'subarea'])->find($id);
        $user_list = UR::where(['room_id' => $id])->column('user_id');
        $bind_user = Bindgame::where([
            "user_id" => ["in", $user_list],
            "game_id" => $room['game_id'],
            "subarea_id" => $room['subarea_id']
        ])->field("game_number,game_nickname,game_avatar,user_id")->select();
        $this->success("", ['room' => $room, 'list' => $bind_user]);
    }


    //我的报名
    public function my_list()
    {
        $game_id = $this->request->param("game_id");
        $pagesize = $this->request->param("size", 10);
        $list = new Users();
        $list->with(["lists"]);
        if ($game_id) {
            $list->where("lists.game_id", $game_id);
        }
        $result = $list->where(['users.user_id' => $this->auth->id])->order("users.createtime DESC")
            ->paginate($pagesize)->each(function ($items) {
                $items->game = $items->lists->game;
                $items->levels = $items->lists->levels;
                $items->modes = $items->lists->modes;
                $items->subares = $items->lists->subarea;
                $items->link = $items->lists->link;
                $items->number = $items->lists->number;
                $items->room_status = $items->lists->status;
                unset($items->lists);
            });
        $this->success("", $result);
    }

    public function check()
    {
        $id = $this->request->param("id", 0);
        $room = Lists::get($id);
        if (!$room) {
            $this->error("房间信息有误");
        }

        $bindname = Bindgame::where(['game_id' => $room->game_id, 'user_id' => $this->auth->id, 'subarea_id' => $room->subarea_id])->find();

        if (!$bindname) {
            $this->error("当前游戏未绑定");
        }

        if ($room->levels->money > $this->auth->money) {
            if ($room->levels->money > $this->auth->gold) {
                $this->error("当前余额不足");
            }
        }

        $this->success("");
    }

    public function my_info()
    {
        $id = $this->request->param("id", 0);

        $user_room = Users::get($id);
        if (!$user_room) {
            $this->error("当前房间信息有误");
        }

        $user = \app\common\model\User::get($user_room['user_id']);

        $list = Lists::with(['game', 'levels', 'modes', 'subarea'])->find($user_room['room_id']);
        $bind = Bindgame::where(['game_id' => $list['game_id'], 'user_id' => $user_room['user_id'], 'subarea_id' => $list['subarea_id']])->find();
        $this->success("", [
            "nickname" => $user['nickname'],
            "game_nickname" => $bind['game_nickname'] ?? "",
            "mobile" => $user['mobile'],
            "mode_name" => $list['modes']['name'],
            "room_id" => $user_room['room_id'],
            "number" => $list['number'],
            "create_text" => $list['create_text'],
            "result" => $user_room['result'],
            "award" => $user_room['award'],
            "game_name" => $list['game']['name'],
            "game_logo" => $list['game']['logo'],
            "game_level" => $list['levels']['money'],
            "game_subarea" => $list['subarea']['name']
        ]);
    }
}